package com.smart.wap.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.smart.common.cache.CacheService;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.member.entities.models.MemberMaterialPigState;
import com.smart.manager.member.entities.models.query.QMemberMaterialPig;
import com.smart.manager.member.entities.models.query.QMemberMaterialPigState;
import com.smart.manager.product.entities.enums.CameraDirection;
import com.smart.manager.product.entities.models.BasicCameraDetail;
import com.smart.manager.product.entities.models.query.QBasicCameraDetail;
import com.smart.manager.product.repository.BasicCameraDetailRepository;
import com.smart.manager.work.entities.assembler.WorkOrder;
import com.smart.manager.work.entities.enums.PigOrderKind;
import com.smart.manager.work.entities.enums.PigOrderStatus;
import com.smart.manager.work.entities.models.PigOrder;
import com.smart.manager.work.entities.models.PigOrderFoods;
import com.smart.manager.work.entities.models.query.QPigOrder;
import com.smart.manager.work.entities.models.query.QPigOrderFoods;
import com.smart.wap.bodys.SharePigResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class ShareService {

    private final BasicCameraDetailRepository basicCameraDetailRepository;
    private final CacheService cacheService;

    public List<String> getOpenCamera() {
        List<BasicCameraDetail> details = new QBasicCameraDetail().status.eq(1).isEncrypt.eq(0).cameraDirection.eq(CameraDirection.ANNOUNCEMENT).setMaxRows(5).findList();
        ArrayList<String> arrayList = new ArrayList<>();
        for (BasicCameraDetail detail : details) {
            arrayList.add(basicCameraDetailRepository.getCameraUrl(detail, false));
        }
        return arrayList;
    }


    public SharePigResult getSharePig(String code) {
        SharePigResult pigResult = cacheService.getSharePigResultCache().get(code);
        if (ObjUtil.isEmpty(pigResult)) {
            SharePigResult result = new SharePigResult();
            Optional<MemberMaterialPig> materialPig = new QMemberMaterialPig().code.eq(code).setMaxRows(1).findOneOrEmpty();
            materialPig.ifPresent(x -> {
                MemberMaterialPig pig = materialPig.get();
                result.setPig(setPig(pig));
                result.setFeedingList(setFeeding(pig));
                result.setTreatList(setTreat(pig));
                result.setServeList(setServe(pig));
                result.setStepWarmList(setStepWarm(pig));
            });
            pigResult = result;
            cacheService.getSharePigResultCache().put(code, result);
        }
        return pigResult;
    }


    /**
     * 获取猪猪数据
     *
     * @param pig
     * @return {@link SharePigResult.Pig}
     */
    public SharePigResult.Pig setPig(MemberMaterialPig pig) {

        ArrayList<String> arrayList = new ArrayList<>();
        if (ObjUtil.isNotNull(pig.getPigsty()) && ObjUtil.isNotEmpty(pig.getPigsty().getCameraCode())) {
            List<BasicCameraDetail> details = new QBasicCameraDetail().passageId.in(pig.getPigsty().getCameraCode()).findList();
            for (BasicCameraDetail detail : details) {
                arrayList.add(basicCameraDetailRepository.getLive(detail));
            }
        }
        return new SharePigResult.Pig()
                .setCode(pig.getCode())
                .setAvatar(pig.getMember().getAvatar())
                .setDay(pig.getPigLevel().getDay())
                .setCameraUrl(arrayList)
                .setName(pig.getName())
                .setNickName(pig.getMember().getNickName())
                .setId(pig.getMember().getId())
                .setBreedName(pig.getBreed().getName())
                .setStaffName(pig.getStaff().getName());
    }

    /**
     * 获取喂食
     *
     * @param pig
     * @return {@link List}<{@link SharePigResult.Feeding}>
     */
    public List<SharePigResult.Feeding> setFeeding(MemberMaterialPig pig) {
        List<PigOrderFoods> orders = new QPigOrderFoods().pigCodeList.contains(pig.getCode()).kind.eq(PigOrderKind.L3).status.in(PigOrderStatus.COMPLETED, PigOrderStatus.PROGRESS).order().createTime.desc().findList();
        return orders.stream().filter(ObjUtil::isNotNull).map(c -> new SharePigResult.Feeding()
                        .setVideo(c.getVideo())
                        .setStaffName(c.getStaff().getName())
                        .setAvatar(c.getStaff().getAvatar())
                        .setCompleteTime(c.getCompleteTime())
                        .setCreateTime(c.getCreateTime())
                        .setStatus(c.getStatus())
                        .setCode(c.getCode()).setContent(BeanUtil.copyProperties(c.getContent(), WorkOrder.FeedBody.class)))
                .collect(Collectors.toList());
    }

    /**
     * 获取治疗
     *
     * @param pig
     * @return {@link List}<{@link SharePigResult.Treat}>
     */
    public List<SharePigResult.Treat> setTreat(MemberMaterialPig pig) {
        List<PigOrder> orders = new QPigOrder().pig.id.eq(pig.getId()).kind.eq(PigOrderKind.L4).order().createTime.desc().findList();
        return orders.stream().filter(ObjUtil::isNotNull).map(c -> new SharePigResult.Treat()
                        .setStaffName(c.getStaff().getName())
                        .setAvatar(c.getStaff().getAvatar())
                        .setCompleteTime(c.getCompleteTime())
                        .setCreateTime(c.getCreateTime())
                        .setStatus(c.getStatus())
                        .setCode(c.getCode())
                        .setContent((String) c.getContent()))
                .collect(Collectors.toList());
    }


    /**
     * 获取服务
     *
     * @param pig
     * @return {@link List}<{@link SharePigResult.Serve}>
     */
    public List<SharePigResult.Serve> setServe(MemberMaterialPig pig) {
        List<PigOrder> orders = new QPigOrder().pig.id.eq(pig.getId()).kind.eq(PigOrderKind.L5).status.eq(PigOrderStatus.COMPLETED).order().createTime.desc().findList();
        return orders.stream().filter(ObjUtil::isNotNull).map(c -> new SharePigResult.Serve()
                        .setStaffName(c.getStaff().getName())
                        .setAvatar(c.getStaff().getAvatar())
                        .setCompleteTime(c.getCompleteTime())
                        .setCreateTime(c.getCreateTime())
                        .setCode(c.getCode())
                        .setServeName(BeanUtil.copyProperties(c.getContent(), WorkOrder.ServiceTicketBody.class).getCombo().getName())
                        .setIcon(BeanUtil.copyProperties(c.getContent(), WorkOrder.ServiceTicketBody.class).getCombo().getIcon()))
                .collect(Collectors.toList());
    }

    public List<SharePigResult.StepWarm> setStepWarm(MemberMaterialPig pig) {
        List<MemberMaterialPigState> states = new QMemberMaterialPigState().pigId.eq(pig.getId()).order().timing.desc().findList();
        return states.stream().filter(ObjUtil::isNotNull).map(x -> new SharePigResult.StepWarm()
                        .setDate(x.getTiming())
                        .setWarm(x.getTemp())
                        .setStep(x.getStep()))
                .collect(Collectors.toList());
    }

}
